﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

public static class EnumExtensions
{
    /// <summary>
    /// 获取枚举值的描述信息
    /// </summary>
    /// <param name="value">枚举值</param>
    /// <returns>描述信息</returns>
    public static string GetDescription(this Enum value)
    {
        // 获取枚举类型
        Type enumType = value.GetType();

        // 获取枚举值的名称
        string name = Enum.GetName(enumType, value);
        if (name == null)
        {
            return value.ToString();
        }

        // 获取字段信息
        FieldInfo field = enumType.GetField(name);
        if (field == null)
        {
            return value.ToString();
        }

        // 获取描述特性
        DescriptionAttribute attribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false)
                                             .FirstOrDefault() as DescriptionAttribute;

        // 返回描述信息
        return attribute?.Description ?? value.ToString();
    }
}